'use strict';

import HTTPUtil from './HTTPUtil'
import Global from './GlobalConst';
import Toast from '../Utils/toast';
export default class APIService {
    /**
     * 免密码登录
     */
    static loginFree(phone, code, registered_id) {
        console.log(phone, code, registered_id);
        var url = Global.HOST + '/User/loginFree';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("phone", phone);
            formData.append("code", code);
            formData.append("registered_id", registered_id);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('参数不能为空');
                    } else if (json.code == 101) {
                        Toast.show('手机号格式不正确！');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 账号密码登录
     */
    static login(phone, password, registered_id) {
        var url = Global.HOST + '/User/login';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("phone", phone);
            formData.append("password", password);
            formData.append("registered_id", registered_id);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('参数不能为空');
                    } else if (json.code == 101) {
                        Toast.show('手机号格式不正确！');
                    } else if (json.code == 102) {
                        Toast.show('密码格式不正确！');
                    } else if (json.code == 103) {
                        Toast.show('账号不存在！');
                    } else if (json.code == 104) {
                        Toast.show('账号或密码错误！');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 第三方登录
     */
    static thirdLogin(openid, user_name, head_picture, registered_id) {
        var url = Global.HOST + '/User/thirdLogin';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("openid", openid);
            formData.append("user_name", user_name);
            formData.append("head_picture", head_picture);
            formData.append("registered_id", registered_id);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('参数不能为空');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 个人资料
     */
    static userInfo(token, registered_id) {
        var url = Global.HOST + '/User/userInfos';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("token", token);
            formData.append("registered_id", registered_id);
            HTTPUtil.post(url, formData).then((json) => {
                if (json.code == true) {
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('参数不能为空');
                    } else if (json.code == 105) {
                        Toast.show('账号异地登录异常！');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 获取验证码
     */
    static getCode(phone) {
        var url = Global.HOST + '/User/getCode';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("phone", phone);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    Toast.show('验证码已发送到您的手机上！');
                    resolve(json.result);
                } else {
                    if (json.code == 101) {
                        Toast.show('参数不能为空');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 更新个人资料
     */
    static updateUserInformation(token, registered_id, user_name, head_picture) {
        var url = Global.HOST + '/User/updateUserInformation';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("token", token);
            formData.append("registered_id", registered_id);
            if (user_name != null && user_name != '') {
                formData.append("user_name", user_name);
            }
            if (head_picture != null && head_picture != '') {
                let file = {uri: 'file://' + head_picture, type: 'multipart/form-data', name: 'Picture_06_Space.jpg'};
                formData.append("head_picture", file);
            }
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    Toast.show('更新资料成功');
                    resolve(json.result);
                } else {
                    if (json.code == 101) {
                        Toast.show('参数不能为空');
                    } else if (json.code == 105) {
                        Toast.show('账号异地登录异常！');
                    }
                    Toast.show('网络错误');
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 重置更新密码
     */
    static changePassword(token, registered_id, phone, password, code) {
        var url = Global.HOST + '/User/changePassword';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("token", token);
            formData.append("registered_id", registered_id);
            formData.append("phone", phone);
            formData.append("password", password);
            formData.append("code", code);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    Toast.show('修改密码成功');
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('参数不能为空');
                    } else if (json.code == 101) {
                        Toast.show('手机格式不正确！');
                    } else if (json.code == 102) {
                        Toast.show('手机号码不一致！');
                    } else if (json.code == 103) {
                        Toast.show('验证码错误！');
                    } else if (json.code == 104) {
                        Toast.show('验证码超时！');
                    } else if (json.code == 105) {
                        Toast.show('手机号码不存在！');
                    } else if (json.code == 106) {
                        Toast.show('密码不能为空！');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }


    /**
     * 忘记密码
     */
    static forget(phone, password, code) {
        var url = Global.HOST + '/User/forget';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("phone", phone);
            formData.append("password", password);
            formData.append("code", code);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    Toast.show('修改密码成功');
                    resolve(json.result);
                } else {
                    if (json.code == 100) {
                        Toast.show('参数不能为空');
                    } else if (json.code == 101) {
                        Toast.show('手机格式不正确！');
                    } else if (json.code == 102) {
                        Toast.show('手机号码不一致！');
                    } else if (json.code == 103) {
                        Toast.show('验证码错误！');
                    } else if (json.code == 104) {
                        Toast.show('验证码超时！');
                    } else if (json.code == 105) {
                        Toast.show('手机号码不存在！');
                    } else if (json.code == 106) {
                        Toast.show('密码不能为空！');
                    } else if (json.code == 107) {
                        Toast.show('手机号码不存在！');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 我的问答列表
     */
    static questionAnswerList(token, registered_id, type, page) {
        var url = Global.HOST + '/User/questionAnswerList';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("token", token);
            formData.append("registered_id", registered_id);
            formData.append("type", type);
            formData.append("page", page);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json);
                } else {
                    if (json.code == 100) {
                        if (type == '0') {
                            Toast.show('暂无数据！');
                        }
                    } else if (json.code == 105) {
                        Toast.show('账号异地登录异常！');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 我的游记列表
     */
    static travelList(token, registered_id, page) {
        var url = Global.HOST + '/User/travelList';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("token", token);
            formData.append("registered_id", registered_id);
            formData.append("page", page);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json);
                } else {
                    if (json.code == 100) {
                        Toast.show('暂无数据！');
                    } else if (json.code == 105) {
                        Toast.show('账号异地登录异常！');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 我的约伴列表
     */
    static datingPartnerList(token, registered_id, type, page) {
        var url = Global.HOST + '/User/datingPartnerList';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("token", token);
            formData.append("registered_id", registered_id);
            formData.append("type", type);
            formData.append("page", page);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json);
                } else {
                    if (json.code == 100) {
                        if (type == '0') {
                            Toast.show('暂无数据！');
                        }
                    } else if (json.code == 105) {
                        Toast.show('账号异地登录异常！');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 我的帮购列表
     */
    static helpToBuyList(token, registered_id, type, page) {
        var url = Global.HOST + '/User/helpToBuyList';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("token", token);
            formData.append("registered_id", registered_id);
            formData.append("type", type);
            formData.append("page", page);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json);
                } else {
                    if (json.code == 100) {
                        if (type == '0') {
                            Toast.show('暂无数据！');
                        }
                    } else if (json.code == 105) {
                        Toast.show('账号异地登录异常！');
                    } else {
                        Toast.show('网络错误');
                    }
                    reject(new Error(json));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 我的收藏列表
     */
    static collectionList(token, registered_id, page) {
        var url = Global.HOST + '/User/collectionList';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("token", token);
            formData.append("registered_id", registered_id);
            formData.append("page", page);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json);
                }
                if (json.code == 100) {
                    resolve(json);
                    Toast.show('暂无数据！');
                } else if (json.code == 105) {
                    Toast.show('账号异地登录异常！');
                } else {
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 意见反馈
     */
    static feedback(token, registered_id, content) {
        var url = Global.HOST + '/User/feedback';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("token", token);
            formData.append("registered_id", registered_id);
            formData.append("content", content);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json.result);
                } else {

                    Toast.show(Global.SERVERERR);
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }

    /**
     * 添加意见反馈图片
     */
    static feedbackImg(token, registered_id, cid, images) {
        var url = Global.HOST + '/User/feedbackImg';
        return new Promise(function (resolve, reject) {
            let formData = new FormData();
            formData.append("token", token);
            formData.append("registered_id", registered_id);
            formData.append("cid", cid);
            let file = {uri: 'file://' + images, type: 'multipart/form-data', name: 'images.jpg'};
            formData.append("images", file);
            HTTPUtil.post(url, formData).then((json) => {
                console.log(url, json);
                if (json.code == true) {
                    resolve(json.result);
                } else {
                    Toast.show(Global.SERVERERR);
                    reject(new Error(json.result));
                }
            }).catch((err)=> {
                Toast.show(Global.SERVERERR);
                reject(new Error(Global.SERVERERR));
            });
        });
    }
}